clear all; clc;close all;
%Updated: 03/29/217

%Compute impulse-response functions using finer endowment and debt grids
% #endowment gridpoints (na2)=500, #debt gridpoints (nb2)=1500
% Initial endowment level: uncoditional mean
% Feed in one-std-deviation shock in period 1

lambda = 0.184;
rf     = 0.02;
cd 'irf'              %initial debt: mean from asymptotic debt dist

%case 1
%cd 'irf_meanb'              %initial debt: mean from asymptotic debt dist
cd 'irfmc_0.75bss'              %initial debt: mean from asymptotic debt dist

p   =load('irfp.txt');
h   =load('irfh.txt');
b   =load('irfb.txt');
ct  =load('irfct.txt');
r   =load('irfr.txt');
%m   =load('irfm.txt');
gn  =load('irfgn.txt');
tau =load('irftau.txt');
a   =load('irfa.txt');
cn  =load('irfcn.txt');
ww  =load('irfww.txt');
bor =load('irfbor.txt');

p0   =p(:,1);
h0   =h(:,1);
b0   =b(:,1);
ct0  =ct(:,1);
r0   =r(:,1);
gn0  =gn(:,1);
tau0 =tau(:,1);
a0   =a(:,1);
cn0  =cn(:,1);
ww0  =ww(:,1);
bor0 =bor(:,1);

%case 4
cd ..
%cd 'irf_meanb'               %initial debt: 1.5*mean from asymptotic debt dist
cd 'irfmc_bss'               %initial debt: 1.5*mean from asymptotic debt dist

p   =load('irfp.txt');
h   =load('irfh.txt');
b   =load('irfb.txt');
ct  =load('irfct.txt');
r   =load('irfr.txt');
gn  =load('irfgn.txt');
tau =load('irftau.txt');
a   =load('irfa.txt');
cn  =load('irfcn.txt');
ww  =load('irfww.txt');
bor =load('irfbor.txt');

p15   =p(:,1);
h15   =h(:,1);
b15   =b(:,1);
ct15  =ct(:,1);
r15   =r(:,1);
gn15  =gn(:,1);
tau15 =tau(:,1);
a15   =a(:,1);
cn15  =cn(:,1);
ww15  =ww(:,1);
bor15 =bor(:,1);


% cd ..
% 
b   = -b;   %/(lambda+rf);
b0  = -b0; %/(lambda+rf);
b15 = -b15; %/(lambda+rf);

% r   = 100*r;
% r0  = 100*r0;
% r05 = 100*r05;
% r15 = 100*r15;

nirf = 20;      %number of periods
time = 1:nirf;

NameArray = {'LineStyle','Color','LineWidth'};
ValueArray = {'--','-','-.';[0.23137255012989 0.443137258291245 0.337254911661148],[0.078  0.1686  0.549],[1  0.4  0.4];1.25,1.25,1.25}';

endt=15;

%if (max(bor(:,1))>0);
%    disp('Differences in access/borrowing paths in case 1');
%elseif (max(bor0(:,1))>0);
%    disp('Differences in access/borrowing paths in case 2');
%elseif (max(bor05(:,1))>0);
%    disp('Differences in access/borrowing paths in case 3');
%elseif (max(bor15(:,1))>0);
%    disp('Differences in access/borrowing paths in case 4');
%end;

NameArray = {'LineStyle','Color','LineWidth'};
ValueArray2 = {':','-';[0.078  0.1686  0.549],[1  0.4  0.4];1.25,1.25}';



figure;
subplot(4,2,1);P=plot(time,a(1:nirf,1),time,a(1:nirf,1));title('y^T (y_0=y^{T,ss}+\sigma)','Fontweight','normal');
AX1 = gca;box on;
set(AX1,'YTick',-0.04:0.02:0);
axis(AX1, [0 time(endt) -0.04 0]);
set(P,NameArray,ValueArray2);
subplot(4,2,2);P=plot(time,-100*h0(1:nirf,1),time,-100*h15(1:nirf,1));title('unemployment','Fontweight','normal');
%lgd=legend('low debt', 'intermediate debt','high debt');lgd.Location='SouthEast';
legend('low debt (0.75b^{ss})','high debt (1.25b^{ss})','Location','NorthEast');ylabel('%');
set(P,NameArray,ValueArray2);
AX1 = gca;box on;
set(AX1,'YTick',0:5:10);
axis(AX1, [0 time(endt) -0.4 10]);
subplot(4,2,3);P=plot(time,p0(1:nirf,1),time,p15(1:nirf,1));title('p^N','Fontweight','normal');
AX1 = gca;box on;
set(AX1,'YTick',-0.1:0.05:0);
axis(AX1, [0 time(endt) -0.1 0.01]);
set(P,NameArray,ValueArray2);
subplot(4,2,4);P=plot(time,gn0(1:nirf,1),time,gn15(1:nirf,1));title('g^N','Fontweight','normal');
AX1 = gca;box on;
set(AX1,'YTick',-0.03:0.01:0.01);
axis(AX1, [0 time(endt) -0.03 0.01]);
set(P,NameArray,ValueArray2);
subplot(4,2,5);P=plot(time,b0(1:nirf,1),time,b15(1:nirf,1));title('debt','Fontweight','normal');
set(P,NameArray,ValueArray2);
AX1 = gca;box on;
set(AX1,'YTick',-0.08:0.08:0.08);
axis(AX1, [0 time(endt) -0.08 0.08]);
subplot(4,2,6);P=plot(time,100*r0(1:nirf,1),time,100*r15(1:nirf,1));title('spreads','Fontweight','normal');ylabel('%');
set(P,NameArray,ValueArray2);
AX1 = gca;box on;
set(AX1,'YTick',0.0:1:3);
axis(AX1, [0 time(endt) -0.05 3]);
subplot(4,2,7);P=plot(time,ct0(1:nirf,1),time,ct15(1:nirf,1));title('c^T','Fontweight','normal');
set(P,NameArray,ValueArray2);
AX1 = gca;box on;
set(AX1,'YTick',-0.08:0.04:0.0);
axis(AX1, [0 time(endt) -0.08 0.004]);
subplot(4,2,8);P=plot(time,cn0(1:nirf,1),time,cn15(1:nirf,1));title('c^N','Fontweight','normal');
set(P,NameArray,ValueArray2);
AX1 = gca;box on;
set(AX1,'YTick',-0.06:0.02:0);
axis(AX1, [0 time(endt) -0.06 0.002]);

dbstop;

figure;
subplot(4,2,1);P=plot(time,a(1:nirf,1),time,a(1:nirf,1),time,a(1:nirf,1));title('y^T','Fontweight','normal');
set(P,NameArray,ValueArray);
subplot(4,2,2);P=plot(time,-h0(1:nirf,1),time,-h05(1:nirf,1),time,-h15(1:nirf,1));title('unemployment','Fontweight','normal');
%lgd=legend('low debt', 'intermediate debt','high debt');lgd.Location='SouthEast';
legend('low debt', 'intermediate debt','high debt','Location','NorthEast');
set(P,NameArray,ValueArray);
subplot(4,2,3);P=plot(time,p0(1:nirf,1),time,p05(1:nirf,1),time,p15(1:nirf,1));title('p^N','Fontweight','normal');
set(P,NameArray,ValueArray);
subplot(4,2,4);P=plot(time,gn0(1:nirf,1),time,gn05(1:nirf,1),time,gn15(1:nirf,1));title('g^N','Fontweight','normal');
set(P,NameArray,ValueArray);
subplot(4,2,5);P=plot(time,b0(1:nirf,1),time,b05(1:nirf,1),time,b15(1:nirf,1));title('debt','Fontweight','normal');
set(P,NameArray,ValueArray);
subplot(4,2,6);P=plot(time,r0(1:nirf,1),time,r05(1:nirf,1),time,r15(1:nirf,1));title('spreads','Fontweight','normal');
set(P,NameArray,ValueArray);
subplot(4,2,7);P=plot(time,ct0(1:nirf,1),time,ct05(1:nirf,1),time,ct15(1:nirf,1));title('c^T','Fontweight','normal');
set(P,NameArray,ValueArray);
subplot(4,2,8);P=plot(time,cn0(1:nirf,1),time,cn05(1:nirf,1),time,cn15(1:nirf,1));title('c^N','Fontweight','normal');
set(P,NameArray,ValueArray);


